{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unified Medical Language (UMLS) filtering\n",
    "\n",
    "This example demonstrates how to obtain an occurrance matrix associated with a\n",
    "a UMLS concept in a previously obtained pubmed/medline search\n",
    "\n",
    "**Note:** This example uses the database created and updated by the following scripts/notebooks:\n",
    "* examples/pubmed_search_and_save\n",
    "* exmaples/pubmed_mesh_to_umls_map\n",
    "\n",
    "The following backends are supported for retieving the prior information:\n",
    "\n",
    "* [MySQL](#MySQL)\n",
    "* [SQLite](#SQLite)\n",
    "\n",
    "Author: Isabel Restrepo <br>\n",
    "BCBI - Brown University <br>\n",
    "Version: Julia 0.6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Method definition ==(Base.Nullable{S}, Base.Nullable{T}) in module Base at nullable.jl:238 overwritten in module NullableArrays at /Users/isa/.julia/v0.6/NullableArrays/src/operators.jl:99.\n"
     ]
    }
   ],
   "source": [
    "using BioMedQuery.Processes\n",
    "using BioMedQuery.PubMed\n",
    "using MySQL\n",
    "using SQLite"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "results_dir = \"./results\"\n",
    "umls_concept = \"Disease or Syndrome\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id='MySQL'></a>\n",
    "## MySQL backend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Filter mesh query string : SELECT mesh FROM mesh2umls WHERE umls IN ( \"Disease or Syndrome\" )\n",
      "-------------------------------------------------------------\n",
      "Found 5 articles with valid descriptors\n",
      "-------------------------------------------------------------\n",
      "  1.387336 seconds (630.19 k allocations: 34.277 MiB, 0.61% gc time)\n",
      "-------------------------------------------------------------\n",
      "Output Descritor to Index Dictionary\n",
      "Dict(\"Obesity\"=>1,\"Pediatric Obesity\"=>2,\"Sleep Apnea, Obstructive\"=>3,\"Metabolic Syndrome\"=>4,\"Fibromyalgia\"=>5)\n",
      "-------------------------------------------------------------\n",
      "-------------------------------------------------------------\n",
      "Output Data Matrix\n",
      "[1.0 1.0 0.0 1.0 0.0; 0.0 0.0 1.0 0.0 0.0; 0.0 1.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 1.0; 1.0 0.0 0.0 0.0 0.0]\n",
      "-------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "host=\"127.0.0.1\" \n",
    "mysql_usr=\"root\"\n",
    "mysql_pswd=\"\"\n",
    "dbname=\"pubmed_obesity_2010_2012\"\n",
    "\n",
    "db_mysql = mysql_connect(host, mysql_usr, mysql_pswd, dbname)\n",
    "\n",
    "@time labels2ind, occur = umls_semantic_occurrences(db_mysql, umls_concept)\n",
    "\n",
    "\n",
    "println(\"-------------------------------------------------------------\")\n",
    "println(\"Output Descritor to Index Dictionary\")\n",
    "println(labels2ind)\n",
    "println(\"-------------------------------------------------------------\")\n",
    "\n",
    "println(\"-------------------------------------------------------------\")\n",
    "println(\"Output Data Matrix\")\n",
    "println(full(occur))\n",
    "println(\"-------------------------------------------------------------\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id='SQLite'></a>\n",
    "## SQLite backend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Filter mesh query string : SELECT mesh FROM mesh2umls WHERE umls IN ( \"Disease or Syndrome\" )\n",
      "-------------------------------------------------------------\n",
      "Found 5 articles with valid descriptors\n",
      "-------------------------------------------------------------\n",
      "  1.939182 seconds (472.07 k allocations: 26.075 MiB, 0.39% gc time)\n",
      "-------------------------------------------------------------\n",
      "Output Descritor to Index Dictionary\n",
      "Dict(\"Obesity\"=>1,\"Sleep Apnea, Obstructive\"=>2,\"Pediatric Obesity\"=>3,\"Metabolic Syndrome\"=>4,\"Fibromyalgia\"=>5)\n",
      "-------------------------------------------------------------\n",
      "-------------------------------------------------------------\n",
      "Output Data Matrix\n",
      "[1.0 1.0 0.0 1.0 0.0; 0.0 1.0 0.0 0.0 0.0; 0.0 0.0 1.0 0.0 0.0; 0.0 0.0 0.0 0.0 1.0; 1.0 0.0 0.0 0.0 0.0]\n",
      "-------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "db_path=\"$(results_dir)/pubmed_obesity_2010_2012.db\"\n",
    "db_sqlite = SQLite.DB(db_path)\n",
    "@time labels2ind, occur = umls_semantic_occurrences(db_sqlite, umls_concept)\n",
    "\n",
    "println(\"-------------------------------------------------------------\")\n",
    "println(\"Output Descritor to Index Dictionary\")\n",
    "println(labels2ind)\n",
    "println(\"-------------------------------------------------------------\")\n",
    "\n",
    "println(\"-------------------------------------------------------------\")\n",
    "println(\"Output Data Matrix\")\n",
    "println(full(occur))\n",
    "println(\"-------------------------------------------------------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.0",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
